package com.zmn.plat.admin.controller.product.graphic;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.model.entity.product.graphic.ProductGraphic;
import com.zmn.plat.services.interfaces.product.graphic.ProductGraphicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 产品图文基础修改Controller
 *
 * @author yangshuangping
 * @since 2021/12/09 17:41
 */
@Slf4j
@RestController
@RequestMapping("/graphic/")
public class ProductGraphicModifyController extends PlatBaseController {

    @Resource
    private ProductGraphicService productGraphicService;

    /**
     * 描述: 保存
     *
     * @param productGraphic 修改实体
     * @return com.zmn.common.dto2.ResponseDTO
     * @author yangshuangping
     * @since 2021/12/09 17:41
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ProductGraphic productGraphic) {

        Date now = DateUtil.getNow();
        Integer graphicId = productGraphic.getGraphicId();
        try {
            productGraphic.setUpdater(getMcStaffRealName());
            productGraphic.setUpdateTime(now);
            if (NumberUtil.isNullOrZero(graphicId)) {
                productGraphic.setCreater(getMcStaffRealName());
                productGraphic.setCreateTime(now);
                productGraphicService.insert(productGraphic);
            } else {
                productGraphicService.updateByKey(productGraphic);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 修改状态
     *
     * @param ids    id集合
     * @param status 状态
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.Boolean>
     * @author yangshuangping
     * @since 2021/12/09 17:41
     */
    @RequestMapping(value = "/updateStatus")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "ids") String ids,
                                             @RequestParam(value = "status") Integer status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            productGraphicService.updateStatusBatch(arrayIds, status, getMcStaffRealName());
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
            return ResponseDTO.fail("修改状态失败");
        }
    }

    /**
     * 功能描述: 修改排序
     *
     * @param graphicId 主键
     * @param sort      排序
     * @return com.zmn.common.dto2.ResponseDTO<java.lang.Boolean>
     * @author yangshuangping
     * @since 2021/12/09 17:41
     */
    @RequestMapping(value = "/updateSort")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "id") Integer graphicId,
                                             @RequestParam(value = "sort") Integer sort) {

        if (NumberUtil.isNullOrZero(graphicId)) {
            return ResponseDTO.fail("请选择要更改排序的数据");
        }
        if (NumberUtil.isNullOrZero(sort)) {
            return ResponseDTO.fail("请设置排序值");
        }
        try {
            ProductGraphic productGraphic = ProductGraphic.builder()
                    .graphicId(graphicId)
                    .sort(sort)
                    .updater(getMcStaffRealName())
                    .updateTime(DateUtil.getNow())
                    .build();
            productGraphicService.updateByKey(productGraphic);
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            return ResponseDTO.fail("修改排序失败");
        }
    }

}
